import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

const app = getApp();

Page({
    data: {
        member: 0,
        registerList: []
    },
    onShareAppMessage() {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        });
    },
    async onShow() {
        const db = qq.cloud.database();
        const _ = db.command;
        let This = this;
        let signList = [];
        let memberList = [];
        let registerList = [];
        await db.collection("member")
            .where({
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                memberList = res.data;
            });
        await db.collection("sign-in")
            .where({
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                signList = res.data;
            });
        await db.collection("black")
            .where({
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                blockList = res.data;
            });
        for (let i of memberList) {
            let bool = false;
            for (let j of signList) {
                if (i.unionId == j.unionId) {
                    bool = true;
                    break;
                }
            }
            for (let j of blockList) {
                if (i.unionId == j.unionId) {
                    bool = true;
                    break;
                }
            }
            if (!bool) {
                registerList.push({ userName: i.Name, unionId: i.unionId });
            }
        }
        this.setData({ registerList: registerList });
    },
    async retroactive(e) {
        await Dialog.confirm({
                title: '提示',
                message: '请确认是否为本盟成员',
            })
            .then(async() => {
                const db = qq.cloud.database();
                const _ = db.command;
                let admin = false
                await db.collection('administer').where({
                    unionId: _.eq(app.data.unionId)
                }).get().then((res) => {
                    if (res.data.length != 0) {
                        admin = true
                    }
                })
                if (admin) {
                    db.collection('sign-in').add({
                        data: {
                            userName: e.target.dataset.username,
                            unionId: e.target.dataset.id,
                            alliance: app.data.alliance
                        },
                    }).then((res) => {
                        Toast('添加成功');
                        let count = 0;
                        this.data.registerList.map((ev, index) => {
                            if (ev.unionId == e.target.dataset.id) {
                                count = index;
                            }
                        });
                        this.data.registerList.splice(count, 1)
                        this.setData({ registerList: this.data.registerList });
                    })
                } else {
                    Toast('没有管理员权限');
                }
            })
            .catch(() => {
                // on cancel
            });

    },

})